<?php

namespace app\api\controller\answers;

use app\api\model\answers\Template;
use app\api\model\answers\LvyouTemp;
use app\api\model\answers\AnswersEvent;
use app\api\service\token\LoginToken;
use app\lib\exception\answers\AnswersException;
use think\Db;
use think\Exception;
use think\facade\Request;

class Yiyan
{

    protected $Service;
    public function __construct()
    {

    }

    public function getAccessToken(){

        $result = (new \app\lib\yiyan\Yiyan())->getAccessToken();
        return writeJson(200,$result);

    }

    public function turbo(){
        $question = Request::post()['question'];
        $result = (new \app\lib\yiyan\Yiyan())->turbo($question);
        return writeJson(200,$result);
    }

    public function derivativeWork(){
        $params = Request::post();

        $title = $params['title'];
        $body = $params['body'];
        $comment = $params['comment'];

        $result = (new \app\lib\yiyan\Yiyan())->derivativeWork($title,$body,$comment);
        return writeJson(200,$result);
    }

    public function derivativeTitle(){
        $params = Request::post();

        $title = $params['title'];

        $result = (new \app\lib\yiyan\Yiyan())->derivativeTitle($title);
        return writeJson(200,$result);
    }

    public function lightsecondaryTrauma(){
        $params = Request::post();

        $body = $params['body'];

        $result = (new \app\lib\yiyan\Yiyan())->lightsecondaryTrauma($body);
        return writeJson(200,$result);
    }

    public function summaryByComment(){
        $params = Request::post();
        $body = $params['body'];
        $comment = $params['comment'];
        $result = (new \app\lib\yiyan\Yiyan())->summaryByComment($body,$comment);
        return writeJson(200,$result);
    }

    public function sayHello(){
        $params = Request::post();

        $title = $params['title'];

        $result = (new \app\lib\yiyan\Yiyan())->sayHello($title);
        return writeJson(200,$result);
    }

    public function three(){
        $params = Request::post();

        $content = $params['content'];

        $result = (new \app\lib\yiyan\Yiyan())->three($content);
        return writeJson(200,$result);
    }


    public function txt2img(){
        $params = Request::post();
        $content = $params['content'];
        $style = $params['style'];
        $result = (new \app\lib\yiyan\Yiyan())->txt2im($content,$style);
        return writeJson(200,$result);
    }


    public function getImg(){
        $params = Request::post();
        $taskId = $params['taskId'];
        $result = (new \app\lib\yiyan\Yiyan())->getImg($taskId);
        return writeJson(200,$result);
    }


    public function model(){
        $params = Request::post();
        $result = (new \app\lib\yiyan\Yiyan())->model($params['content']);
        return writeJson(200,$result);
    }

    public function getEvent(){
        $info = AnswersEvent::with("template")->where("status", 1)->findOrEmpty();
        if ($info->isEmpty()) {
            $info = [];
        } else {
            $info->save(["status"=>2], ["id"=>$info['id']]);
        }
        return writeJson(200,$info,'数据列表！');
    }

    public function backFill()
    {
        $params = Request::post();
        $info = AnswersEvent::where("id",$params["id"])->findOrEmpty();
        if ($info->isEmpty()) {
            throw new AnswersException(['msg' => "该任务不存在", 'error_code' => 20001]);
        }
        AnswersEvent::update($params,["id"=>$info['id']]);
        return writeJson(200,$info,'回填成功！');
    }

    public function getList()
    {
        $params = Request::post();
        $pageSize = $params['pageSize'] ?? 20;
        $list = Template::paginate($pageSize);
        return writeJson(200,$list,'数据列表！');
    }

    public function addEvent()
    {
        $params = Request::post();
        DB::startTrans();
        try {
            $uid = LoginToken::getInstance()->getCurrentUid()??"";
            $params["uid"] = $uid;
            $params["json_text"] = json_encode($params["json_text"], JSON_UNESCAPED_UNICODE);
            AnswersEvent::create($params);
            Db::commit();
        }catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new AnswersException([
                'msg' => '创建失败',
                'error_code' => 20002
            ]);
        }
        return writeJson(200,[],'创建成功！');
    }

    public function lvyouImage(){
        $params = Request::post();
        $url = "http://127.0.0.1:6000/handleImage";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));// 文本提交方式，必须声明请求头
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output);
    }

    public function lvyouTemp()
    {
        $params = Request::post();
        $pageSize = $params['pageSize'] ?? 20;
        $list = LvyouTemp::paginate($pageSize);
        foreach ($list as &$li) {
            $li["format"] = json_decode($li["format"]);
        }
        return writeJson(200,$list,'数据列表！');
    }

}
